package org.tinygroup.bizframe.business.impl;

import java.util.ArrayList;
import java.util.List;

import org.tinygroup.bizframe.business.inter.SysUserRightBusiness;
import org.tinygroup.bizframe.dao.inter.pojo.complex.TRightRef;
import org.tinygroup.bizframe.ext.dao.inter.TUserRightRefDealDao;
import org.tinygroup.bizframe.ext.dao.inter.TsysUserRightDao;
import org.tinygroup.bizframe.ext.dao.inter.pojo.TsysUserRight;
import org.tinygroup.jdbctemplatedslsession.daosupport.OrderBy;
import org.tinygroup.menuframe.config.Menu;
import org.tinygroup.menuframe.dto.MenuInfo;
import org.tinygroup.menuframe.dto.MenuNode;
import org.tinygroup.menuframe.manager.MenuTreeManager;
import org.tinygroup.menuframe.util.MenuUtil;
import org.tinygroup.tinysqldsl.Pager;

public class SysUserRightBusinessImpl implements SysUserRightBusiness {
	private TsysUserRightDao tsysUserRightDao;
	
	private MenuTreeManager menuTreeManager;

	public MenuTreeManager getMenuTreeManager() {
		return menuTreeManager;
	}

	public void setMenuTreeManager(MenuTreeManager menuTreeManager) {
		this.menuTreeManager = menuTreeManager;
	}

	private TUserRightRefDealDao tUserRightRefDealDao;
	
	public TsysUserRightDao getTsysUserRightDao() {
		return tsysUserRightDao;
	}

	public void setTsysUserRightDao(TsysUserRightDao tsysUserRightDao) {
		this.tsysUserRightDao = tsysUserRightDao;
	}

	public TUserRightRefDealDao gettUserRightRefDealDao() {
		return tUserRightRefDealDao;
	}

	public void settUserRightRefDealDao(TUserRightRefDealDao tUserRightRefDealDao) {
		this.tUserRightRefDealDao = tUserRightRefDealDao;
	}

	public TsysUserRight getById(Integer id) {
		return tsysUserRightDao.getByKey(id);
	}

	public Pager<TsysUserRight> getPager(int start, int limit,
										 TsysUserRight sysUserRight, OrderBy... orderBies) {
		return tsysUserRightDao.queryPager(start, 10000, sysUserRight, orderBies);
	}

	public TsysUserRight add(TsysUserRight sysUserRight) {
		return tsysUserRightDao.add(sysUserRight);
	}

	public int update(TsysUserRight sysUserRight) {
		return tsysUserRightDao.edit(sysUserRight);
	}

	public int deleteByKeys(Integer... pks) {
		return tsysUserRightDao.deleteByKeys(pks);
	}

	public boolean checkExists(TsysUserRight sysUserRight) {
		return tsysUserRightDao.query(sysUserRight).size() == 0 ? false : true;
	}

	public int[] grantRights(String userId,Integer[] menuIds, String createBy) {
		return tUserRightRefDealDao.grantRights(userId,menuIds, createBy);
	}

	public int revokeRights(String userCode,Integer[] userRightIds) {
		return tUserRightRefDealDao.revokeRights(userCode,userRightIds);
	}

	public Pager<TRightRef> queryRightsInUser(int start, int pageSize, String userCode, TRightRef tRightRef, Boolean isAssigned) {
		
		TsysUserRight sysUserRight=new TsysUserRight();
		sysUserRight.setUserId(userCode);
		List<TsysUserRight> lsUserRight = tsysUserRightDao.query(sysUserRight);
		List<Integer> lsMenuId= new ArrayList<Integer>();
		for(TsysUserRight r : lsUserRight){
			lsMenuId.add(r.getMenuId());
		}
		List<MenuNode> authed = menuTreeManager.getAuthMenuNodeList(lsMenuId);
		List<Menu> assigned=null;
		if(null != authed && !authed.isEmpty()){
			MenuInfo menuInfo = new MenuInfo();
	        menuInfo.setMenuList(authed);
			assigned = MenuUtil.toList(menuInfo);
			
		}
		else {
			assigned=new ArrayList<Menu>();
		}
		
		List<TRightRef> lsRight=new ArrayList<TRightRef>();
		
		if(isAssigned){
		   for(Menu m : assigned){
			   for(Integer mId : lsMenuId){
				   if(mId.equals(m.getId())){
					   TRightRef r=new TRightRef();
					   r.setMenuId(m.getId());
					   r.setMenuName(m.getMenuName());
					   lsRight.add(r); 
				   }
			   }
			   
		   }
		   	
		   Pager<TRightRef> pagerAssigned =new Pager<TRightRef>(assigned.size(),start,lsRight);
		   return pagerAssigned;
		}
		else{
			List<Menu> all = menuTreeManager.getAllMenu(null);
			for(Menu a : all){
				boolean hasAssigned=false;
				for(Integer mId : lsMenuId){
				   if(mId.equals(a.getId())){
					   hasAssigned=true;
				   }
				}
				if(!hasAssigned){
					TRightRef r=new TRightRef();
					r.setMenuId(a.getId());
					r.setMenuName(a.getMenuName());
					lsRight.add(r);
				}
			}
			 Pager<TRightRef> pagerNoAssigned =new Pager<TRightRef>(assigned.size(),start,lsRight);
			 return pagerNoAssigned;
		}
		//return tUserRightRefDealDao.queryRightsInUser(start,10000,roleCode,tRightRef,isAssigned);
	}

}
